package com.quad.innovators.salesease.service.impl.tag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.TagConverter;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.StatusEnum;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.tag.TagMapper;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.entity.tag.Tag;
import com.quad.innovators.salesease.model.form.tag.TagForm;
import com.quad.innovators.salesease.model.query.tag.TagQuery;
import com.quad.innovators.salesease.model.vo.tag.TagVO;
import com.quad.innovators.salesease.service.tag.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cheng
 */
@Service
@RequiredArgsConstructor
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    private final TagConverter tagConverter;

    @Override
    public PageResultBean<TagVO> listPaged(TagQuery queryParams) {
        Page<TagVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        Page<TagVO> tagVOPage = this.baseMapper.listPaged(page, queryParams);
        return tagConverter.toPageVo(tagVOPage);
    }

    @Override
    public boolean save(TagForm form) {
        Tag tag = tagConverter.toEntity(form);
        tag.setCode(SystemCodeUtils.getCode(SystemCodeEnum.TAG));
        return this.baseMapper.insert(tag) > 0;
    }

    @Override
    public boolean update(String code, TagForm form) {
        Tag tag = this.baseMapper.selectOne(new LambdaQueryWrapper<Tag>().eq(Tag::getCode, code));
        if (ObjectUtil.isNull(tag)){
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        Tag newTag = tagConverter.toEntity(form);
        newTag.setId(tag.getId());
        return this.baseMapper.updateById(newTag) > 0;
    }

    @Override
    public boolean updateStatus(String code, Integer status) {
        Tag formData = this.baseMapper.selectOne(new LambdaQueryWrapper<Tag>().eq(Tag::getCode, code));
        if(formData == null){
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        formData.setStatus(status);
        this.baseMapper.updateById(formData);
        return true;
    }

    @Override
    public TagForm getFormData(String code) {
        Tag formData = this.baseMapper.selectOne(new LambdaQueryWrapper<Tag>().eq(Tag::getCode, code));
        return tagConverter.toForm(formData);
    }

    @Override
    public List<Option<String>> getList() {
        List<Tag> tags = this.baseMapper.selectList(new LambdaQueryWrapper<Tag>().eq(Tag::getStatus, StatusEnum.ENABLE.getValue()));
        return tags.stream().map(it -> new Option<>(it.getCode(), it.getName())).collect(Collectors.toList());
    }
}
